<template>
    <div class="wrapper">
        <div :style="{position:'relative'}">
            <div class="coruverImage">
                <img
                        ref="coruverImage"
                        :src="$props.shop.image.url||'http://pm0tnn81a.bkt.clouddn.com/defaultShopCover.jpg'"
                        alt="">
            </div>

            <div class="upload-btn" @click="before_upload">
                <input type="file" ref="inpout_file" class="fileinput" @change="fileChange"></input>
                更换背景
            </div>
        </div>
    </div>
</template>
<script>
    import {uploadImage} from "./service";
    import MyAlert from '../../../utils/myalert'
    import {previewImage} from "../../../utils/dom";

    export default {
        props: {
            onChangeFile: {
                type: Function,
            },
            shop: {
                type: Object,
                default: {
                    "desc": "",
                    "name": "",
                    "userId": "",
                    "imageId": "",
                    "shopUrl": "",
                    "objectId": "",
                    "createdAt": "",
                    "updatedAt": "",
                    shopImage: {
                        url: "http://pm0tnn81a.bkt.clouddn.com/defaultShopCover.jpg"
                    }
                }
            }
        },
        mounted() {
        },
        methods: {
            before_upload(e) {
                var self = this
                MyAlert.confirm("将会覆盖原有文件，确定上传？").then(() => {
                    setTimeout(() => {
                        self.$refs['inpout_file'].dispatchEvent(new MouseEvent('click'));
                    }, 500)
                })
            },
            fileChange(e) {
                var self = this
                previewImage(e.target.files[0], this.$refs['coruverImage'])
                MyAlert.loading()
                uploadImage(e.target.files).then(imageFile => {
                    MyAlert.clear()
                    MyAlert.success("上传成功")
                    self.$props.onChangeFile({
                        objectId: imageFile.id,
                        url: imageFile.url()
                    })
                }).catch(e => {
                    setTimeout(() => {
                        MyAlert.clear()
                    }, 500)
                    MyAlert.fail(e.message)
                })
            }
        }
    }
</script>
<style lang="less" scoped>
    .wrapper {
        margin-bottom: 20px;
        .coruverImage {
            height: px2rem(80*2);
            img {
                width: 100%;
                height: 100%;
            }
        }
        .upload-btn {
            position: absolute;
            border: 1px solid orangered;
            padding: 3px;
            font-size: 12px;
            border-radius: 0 0 5px 5px;
            color: orangered;
            background: #fff;
            bottom: -20px;
            left: px2rem(300);
            .fileinput {
                font-size: 100%;
                width: 0;
                height: 0;
                position: absolute;
                background: red;
                /*opacity: 0;*/
            }
        }

        .fields {
            margin-top: 30px;
            .formItem {

                padding-left: px2rem(20);
                padding-right: px2rem(20);
                border-bottom: 1px solid #eee;
                .formItemTitle {
                    font-weight: 600;
                    color: #8e8e8e;
                    width: 100%;
                    margin-top: 5px;
                }
                .input {
                    display: flex;
                    align-items: center;
                    height: px2rem(80);
                    width: 100%;
                    input[type='text'] {
                        border: none;
                        height: px2rem(45);
                        width: 100%;
                        padding-left: 10px;
                    }
                    &.textarea {
                        height: auto;
                        textarea {
                            margin-top: 5px;
                            width: 100%;
                            background: rgba(238, 238, 238, 0.62);
                            border: none;
                            padding: 10px;
                        }
                    }
                }

                &:last-child {
                    border-bottom: none;
                }
            }
        }

        .subbtn {
            display: flex;
            justify-content: center;
            align-items: center;
            width: 80%;
            height: 1rem;
            background: #ff4800;
            align-self: center;
            color: #fff;
            margin: auto;
            border-radius: 50px;
            margin-top: 20px;
        }
    }
</style>